'use strict'
import Room from './room'
import EventEmitter from './EventEmitter'
export default class RoomManage extends EventEmitter {
  constructor () {
    super()
    this.rooms = new Map()
    this.platform = 'http://www.douyu.com/'
  }
  isMobile () {
    const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i
      .test(navigator.userAgent)
    return isMobile
  }
  remove (key) {
    let room = this.rooms.get(key)
    if (room) {
      room.remove()
      this.rooms.delete(key)
    }
  }
  getRank (key, field) {
    var room = this.rooms.get(key)
    if (room) {
      return room.filter(field)
    }
    return null
  }
  getRooms () {
    return Array.from(this.rooms).map((room) => {
      return {
        data: room.data,
        info: room.info,
        gift: room.gift,
        platform: room.platform
      }
    })
  }
  getRoom (key) {
    return this.rooms.get(key)
  }
  hasRoom (key) {
    return this.rooms.has(key)
  }
  getGift (roomId) {
    let room = this.rooms.get(roomId)
    if (room) {
      let gift = room.gift
      var gifts = []
      for (var key in gift) {
        if (gift.hasOwnProperty(key)) {
          gifts.push({
            text: gift[key].name + '总榜',
            value: gift[key].id
          })
        }
      }
      return gifts
    }
    return null
  }
  getDate () {
    let date = new Date()
    return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes()
  }
  addRoom (platform, roomId, fn) {
    if (!fn && typeof fn !== 'function') {
      return
    }
    var self = this
    if (this.rooms.has(this._getKey(platform, roomId))) {
      fn(-2)
      return
    }
    var room = new Room(platform, roomId)
    room.on('init', function (info) {
      if (info && info.error === 0) {
        self.emit('addRoom', room)
        self.rooms.set(self._getKey(platform, info.data.room_id), room)
        let obj = {
          avatar: info.data.avatar,
          roomName: info.data.room_name,
          roomId: info.data.room_id,
          nick: info.data.owner_name,
          platform: platform,
          date: self.getDate()
        }
        fn(0)
        if (window.localStorage) {
          try {
            window.localStorage.setItem('room_'.concat(platform).concat('_').concat(info.data.room_id), JSON.stringify(obj))
          } catch (e) {
          }
        }
      } else {
        fn(-1)
        room.clear()
      }
    })
    room.on('chat', function (msg) {
      self.emit('chat', self._getKey(platform, msg.rid), msg)
    })
  }
  _getKey (platform, roomId) {
    return platform.concat('_').concat(roomId)
  }
}

/* Installation */

RoomManage.installed = false

/**
 * Installation interface.
 * Install the necessary directives.
 */

RoomManage.install = function (Vue, options) {
  /* istanbul ignore if */
  if (RoomManage.installed) {
    return
  }
  Vue.prototype.$RoomManage = new RoomManage()
  RoomManage.installed = true
}
